import { Router } from 'express';
import Payload from '../models/Payload';
var models = require('../sqlModels');
let Page = require('../models/Page');
export default () => {
    let api = Router();

    // expenditureInfo新增测试
    api.post('/create', async (req, res, next) => {
        let expenditureInfoData = req.body;
        try {
        let result = await  models.expenditureInfo.create({
                expenditureto: expenditureInfoData.expenditureto,
                expendituretype: expenditureInfoData.expendituretype,
                expenditurecount: expenditureInfoData.expenditurecount,
                expendituredec: expenditureInfoData.expendituredec,
                expendituretime: expenditureInfoData.expendituretime
            })
            res.status(200).json(new Payload(result));
        } catch (error) {
            next(error);
        }
    });
    
    //获取全部expenditureInfo,支持分页
    api.get('/all', async (req, res, next) => {
        try {
            let condition = {
                raw: true
            };
            if (req.query.currentPage && req.query.numPerPage) {
                var numPerPage = parseInt(req.query.numPerPage);
                var currentPage = parseInt(req.query.currentPage);
                condition.offset = numPerPage * (currentPage - 1);
                condition.limit = numPerPage;
            }

            let result = await models.expenditureInfo.findAndCountAll(condition);
            let resultLength = result.rows.length;
            let page = new Page(result.rows, currentPage || 1,
                resultLength, numPerPage || resultLength, result.count);
            res.json(new Payload(page));

        } catch (error) {
            next(error);
        }
    })


   //更新expenditureInfo
    api.put('/:expenditureInfoId', async(req, res, next) => {
        let expenditureInfoId = req.params.expenditureInfoId;
        let expenditureInfoData = req.body;
       try{
           let result = await models.expenditureInfo.update({  
                expenditureto: expenditureInfoData.expenditureto,
                expendituretype: expenditureInfoData.expendituretype,
                expenditurecount: expenditureInfoData.expenditurecount,
                expendituredec: expenditureInfoData.expendituredec,
                expendituretime: expenditureInfoData.expendituretime
            }, {
            where: {
                id: expenditureInfoId
            }
        })
        res.status(200).json(new Payload(result));
       }catch(error){
           next(error);
       }

    })



//根据ID删除
api.delete('/:expenditureInfoId', async(req, res, next) => {
        let expenditureInfoId = req.params.expenditureInfoId;
       try{
           let result = await models.expenditureInfo.destroy({   
                 where: {
                id: expenditureInfoId
            }
        })
        res.status(200).json(new Payload(result));
       }catch(error){
           next(error);
       }

    })

    return api;
}
